package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.annotation.Login;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.base.user.UserCollect;
import com.atguigu.tingshu.vo.base.user.UserInfo;
import com.atguigu.tingshu.vo.base.user.UserPaidTrack;
import com.atguigu.tingshu.vo.user.UserCollectVo;
import com.atguigu.tingshu.vo.user.UserSubscribeVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user/userInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserInfoApiController {

    @Resource
    private UserInfoService userInfoService;

    @GetMapping("getUserInfo/{id}")
    @Operation(summary = "根据id查询用户信息")
    public Result<UserInfo> getUserInfo(@PathVariable Long id) {
        return Result.ok(userInfoService.getById(id));
    }

    @GetMapping("findUserSubscribePage/{page}/{limit}")
    @Operation(summary = "分页查询订阅信息")
    @Login
    public Result<Page<UserSubscribeVo>> findUserSubscribePage(@PathVariable Long page, @PathVariable Long limit) {
        Long userId = AuthContextHolder.getUserId();
        Page<UserSubscribeVo> userSubscribePage = userInfoService.findUserSubscribePage(userId, page, limit);
        return Result.ok(userSubscribePage);
    }

    @GetMapping("isSubscribe/{albumId}")
    @Operation(summary = "查询用户是否订阅")
    @Login
    public Result<Boolean> isSubscribe(@PathVariable Long albumId) {
        Long userId = AuthContextHolder.getUserId();
        boolean flag = userInfoService.isSubscribe(userId, albumId);
        return Result.ok(flag);
    }

    @Login
    @GetMapping("findUserCollectPage/{page}/{limit}")
    @Operation(summary = "分页查询收藏信息")
    public Result<Page<UserCollectVo>> findUserCollectPage(@PathVariable Long page, @PathVariable Long limit) {
        Long userId = AuthContextHolder.getUserId();
        Page<UserCollectVo> userCollectPage = userInfoService.findUserCollectPage(userId, page, limit);
        return Result.ok(userCollectPage);
    }

    @Login
    @GetMapping("isPayAlbum/{albumId}")
    @Operation(summary = "根据专辑id查询当前用户是否购买此专辑")
    public Result<Boolean> isPayAlbum(@PathVariable Long albumId) {
        boolean flag = userInfoService.isPayAlbum(albumId);
        return Result.ok(flag);
    }

    @Login
    @Operation(summary = "根据专辑id查询当前用户是否购买此专辑下的声音列表")
    @GetMapping("isPayTrack/{albumId}")
    public Result<List<UserPaidTrack>> isPayTrack(@PathVariable Long albumId) {
        List<UserPaidTrack> userPaidTracks = userInfoService.isPayTrack(albumId);
        return Result.ok(userPaidTracks);
    }

    @Login
    @Operation(summary = "根据声音id查询当前声音是否被订阅")
    @GetMapping("isCollect/{trackId}")
    public Result<UserCollect> isCollect(@PathVariable Long trackId) {
        UserCollect userCollect = this.userInfoService.isCollect(trackId);
        return Result.ok(userCollect);
    }

    @PostMapping("updateVipStatus")
    public Result updateVipStatus() {
        this.userInfoService.updateVipStatus();
        return Result.ok();
    }

}

